package com.example.homework2.controller; 

import com.example.homework2.entity.MyImage; 

import com.example.homework2.entity.Student; 

import org.springframework.stereotype.Controller; 

import org.springframework.ui.Model; 

import org.springframework.web.bind.annotation.CookieValue; 

import org.springframework.web.bind.annotation.RequestMapping; 

import javax.servlet.http.HttpServletRequest; 

import javax.servlet.http.HttpSession; 

import java.util.Date; 

import java.util.UUID; 

/** 

* SpringBoot中使用Thymeleaf 

* @author yaojianfeng 

*/ 

@Controller 

@RequestMapping("/thymeleaf") 

public class ThymeleafController { 

/** 

* 跳转文本表达式页面 

* @param model 模型对象,封装数据 

* @param session 会话对象,封装数据 

* @notice model与session异同点可参照博客: 

* https://blog.csdn.net/Hello_ChenLiYan/article/details/106411452 

* @return 

*/ 

@RequestMapping("/varEL") 

public String varEL(Model model, HttpSession session){ 

model.addAttribute("result",100+200); 

Student student01 = new Student(); 

student01.setName("Holland"); 

student01.setPhoneNumber("13137536517"); 

model.addAttribute("student01",student01); 

Student student02 = new Student(); 

student02.setName("Libra"); 

student02.setAddress("河南财经政法大学"); 

session.setAttribute("student02",student02); 

return "thymeleaf_EL"; 

} 

/** 

* 跳转链接表达式页面 

* @return 

*/ 

@RequestMapping("/urlEL") 

public String varEL(){ 

return "url_el"; 

} 

/** 

* 跳转首页 

* @return 

*/ 

@RequestMapping("/toIndex") 

public String toIndex(){ 

return "index"; 

} 

/** 

* 跳转字面值页面 

* @param model 

* @return 

*/ 

@RequestMapping("/literal") 

public String literal(Model model){ 

Student student = new Student(); 

student.setSid(UUID.randomUUID().toString()); 

model.addAttribute("student",student); 

return "literal"; 

} 

/** 

* 跳转数字表达式页面 

* @param model 

* @return 

*/ 

@RequestMapping("/calculate") 

public String calculate(int param01,int param02, Model model){ 

model.addAttribute("a",param01); 

model.addAttribute("b",param02); 

model.addAttribute("c",2); 

Student student = new Student(); 

student.setSid(UUID.randomUUID().toString()); 

model.addAttribute("student",student); 

return "calculate"; 

} 

/** 

* 跳转th 常用属性页面 

* @param model 

* @return 

*/ 

@RequestMapping("/thAttr") 

public String thAttr(Model model, HttpServletRequest request){ 

Student student = new Student(); 

student.setSid(UUID.randomUUID().toString()); 

student.setName("Holland"); 

student.setPhoneNumber("13137536517"); 

student.setAge(18); 

MyImage myImage = new MyImage(); 

myImage.setImgAddress(request.getContextPath()+"/Holland/img/image-20220327233925606.png"); 

myImage.setAltMsg("图片加载中..."); 

model.addAttribute("student",student); 

model.addAttribute("img",myImage); 

model.addAttribute("switchFlag","a"); 

return "thAttr"; 

} 

/** 

* 跳转 常用对象页面 

* @param model 

* @return 

*/ 

@RequestMapping("/objTest") 

public String objTest(Model model, HttpServletRequest request, HttpSession session, 

@CookieValue("JSESSIONID") String jsessionid){ 

Student student = new Student(); 

student.setSid(UUID.randomUUID().toString()); 

student.setBirthday(new Date().toString()); 

student.setName("Holland"); 

model.addAttribute("student",student); 

request.setAttribute("requestTest",18); 

session.setAttribute("id",jsessionid); 

return "objTest"; 

} 

}